package com.wzcl.app.controller;


import com.wzcl.app.dao.model.RefundRequest;
import com.wzcl.app.dao.repository.OrdersRepository;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.RefundRequestDto;
import com.wzcl.app.model.vo.RefundRequestVo;
import com.wzcl.app.service.RefundRequestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/refundRequest")
@Api(value = "申请退款controller", tags = {"申请退款相关接口"})
public class RefundRequestController{
    @Resource
    private RefundRequestService service;

    @ApiOperation("申请退款")
    @PostMapping("/add")
    public RVO<RefundRequestVo> addRefundRequest(@RequestBody @Valid RefundRequestDto refundRequestDto){
        return service.addRefundRequest(refundRequestDto);
    }

    @ApiOperation("确认/驳回退款")
    @PostMapping("/audit")
    public RVO<RefundRequestVo> auditRefundRequest(@RequestBody RefundRequestDto refundRequestDto){
        return service.auditRefundRequest(refundRequestDto);
    }

    @ApiOperation("获得申请List")
    @PostMapping("/getList")
    public RVO<List<RefundRequestVo>> getRefundRequestList(@RequestBody RefundRequestDto refundRequestDto){
        List<RefundRequest> list = service.getRefundRequestList(refundRequestDto);
        if(list != null && list.size()>0){
            List<RefundRequestVo> collect = list.stream().map(RefundRequestVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
        return RVO.success(null);
    }

    @ApiOperation("获得申请page")
    @PostMapping("/getPage")
    public RVO<List<RefundRequestVo>> getRefundRequestPage(@RequestBody RefundRequestDto refundRequestDto){
        MyPage<RefundRequest> page = service.getRefundRequestPage(refundRequestDto);
        if(page != null && page.getContent().size()>0){
            List<RefundRequestVo> collect = page.getContent().stream().map(RefundRequestVo::create).collect(Collectors.toList());
            return RVO.success(collect,page);
        }
        return RVO.success(null);
    }


}
